home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1602 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.1 KB

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: IsA v/s HasA problem
  5. Date: 11 Jan 1996 20:56:52 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan11125652@more.eng.sun.com>
  8. References: <30F52C66.851@bangate.compaq.com>
  9. NNTP-Posting-Host: more.eng.sun.com
  10. In-reply-to: Saurabh Dixit's message of Thu, 11 Jan 1996 15:27:34 GMT
  11.  
  12. In article <30F52C66.851@bangate.compaq.com> Saurabh Dixit <saurabhd@bangate.compaq.com> writes:
  13.  
  14. >   Newsgroups: comp.lang.c++
  15. >   From: Saurabh Dixit <saurabhd@bangate.compaq.com>
  16. >   Organization: Compaq Computer Corporation
  17. >   Date: Thu, 11 Jan 1996 15:27:34 GMT
  18. >   X-Nntp-Posting-Host: 172.18.176.8
  19. >
  20. >   Hi!
  21. >
  22. >   I always had no trouble distinguishing between the two...
  23. >
  24. >   But now, I for a specific case, I fail to see the repercussions of
  25. >   using the wrong relationship between objects.
  26. >
  27. >   This is my scenario.
  28. >   I have an object of type Base.  This is an abstract class.
  29. >   From Base I derive several different objects.
  30. >   I want to maintain a list of such objects for which I have a class
  31. >   Collection that encapsulates a typed array of Base objects.
  32. >
  33. >   I now want to define an object - let's call it Duh - that maintains a 
  34. >   collection of Base objects.  Duh objects are entities on their own and could
  35. >   have other attributes, BUT NEVER ANOTHER COLLECTION.
  36. >
  37. >   From what I gather, I am then leaning towards a HasA relationship, i.e.
  38. >   class Collection
  39. >   {
  40. >   public:
  41. >       BOOL Add (Base* b);
  42. >   };
  43. >   class Duh 
  44. >   {
  45. >   private:
  46. >       Collection c;
  47. >   };
  48. >
  49. >   Obviously if I was going to have 2 or more collections, then I would have had
  50. >   to have a HasA relationship.  But as said earlier, this is not the case!
  51. >   Also, I want to have some operations common between Collection and Duh.
  52. >   e.g. I can Add() a Base (derived!) object to a Collection and I also want
  53. >   to Add() an object to Duh.  In a HasA relationship I would simply 
  54. >   implemenent this as follows.
  55. >   BOOL Duh::Add (Base* b)
  56. >   {
  57. >       // simply pass on to Collection method
  58. >       return c.Add (b);
  59. >   }
  60. >   However for an IsA relationship, I would simply do
  61. >   class Duh : public Collection 
  62. >   {
  63. >   };
  64. >   and I am done.
  65. >
  66. >   Am I wasting my time too much thinking about this?  Regardless of code
  67. >   implementation, from the logical perspective, what is the correct object-
  68. >   oriented solution for my problem, HasA or IsA?
  69. >
  70. >   Any help will be greatly appreciated
  71. >   thanks
  72. >   saurabh
  73. >   -- 
  74. >   Saurabh Dixit
  75. >   "All opinions are always my own..."
  76. >   Compaq Computer Corporation
  77. >   ------------------------------------------------------------------------
  78. >   Thou who sneezes without kerchief takes matters in own hands.
  79. >               - SomeoneIOnceNew
  80. >   ------------------------------------------------------------------------ 
  81. >   xxxx
  82. >
  83.  
  84. IsA or HasA releationship (in this example) depends on what other things Duh
  85. does.  Is it adding more functionality to the Collection?  For example, it
  86. could have additional attributes to keep track of highest/lowest value in the
  87. collection, or provide methods to operate on the Collection.  In this case use
  88. the IsA relationship AND DON'T add more things to Duh which has nothing to do
  89. with the Collection.  Define another class for that.  Keep your classes small
  90. and simple with a well defined purpose.  Don't overload it.
  91.  
  92. If Duh needs to keep a Collection for other things that it needs to do then
  93. use the HasA relationship.
  94.  
  95. Another approach is to ask yourselves whether somebody can use the class Duh
  96. in place of the Collection class (with improved functionality)?  If yes, then
  97. use the IsA relationship.
  98.  
  99. If you keep your classes small and simple with well defined purpose then it is
  100. easy to choose between IsA and HasA relationship.
  101.  
  102. Hope this helps.
  103.  
  104. -Nitin
  105. -- 
  106. ----------------------------------------------------------------------
  107. Nitin More                                                         
  108. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  109. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  110. ----------------------------------------------------------------------
  111.